/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
// START SNIPPET: service
package com.joeshing.ws;

import java.util.Date;

import javax.annotation.Resource;
import javax.jws.WebService;

import org.apache.log4j.Logger;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.joeshing.security.dao.AppLogDaoImpl;
import com.joeshing.security.dao.LogDaoImpl;
import com.joeshing.security.dao.UserDaoImpl;
import com.joeshing.security.model.AppLog;


@Component
// @WebService
@WebService(serviceName = "HelloService", endpointInterface = "com.joeshing.ws.HelloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService {

	@Resource
	private AppLogDaoImpl appLogDaoImpl;
	@Resource
	private LogDaoImpl logDaoImpl;
	@Resource
	private UserDaoImpl userDaoImpl;

	private final Logger logger = Logger.getLogger(this.getClass());

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public String sayHi(String text) {
		try {
			return "Hello " + test();
		} catch (InvalidDataAccessApiUsageException e) {
			logger.error("Transactional is readOnly!");
			return "Transactional is readOnly!";
		}
    }

	public long test() {
		logger.info("start testing...");
		Date start = new Date();
		logger.info("get user size=" + userDaoImpl.queryAll().size());
		Date end = new Date();
		logger.info("time: " + (end.getTime() - start.getTime()));
		logger.info("get logs size=" + appLogDaoImpl.queryAll().size());
		logger.info("new appLog...");
		AppLog appLog = new AppLog();
		appLogDaoImpl.insert(appLog);
		logger.info("save appLog...");
		logger.info("id=" + appLog.getId());
		logger.info("delete appLog...");
		// appLogDaoImpl.delete(appLog);
		logger.info("end testing...");
		return end.getTime() - start.getTime();
	}
}
// END SNIPPET: service
